package com.gitee.aurora.test.service;

import com.balloon.springboot.datasource.aop.DataSourceRouteKey;
import com.gitee.aurora.comm.AuroraJdbcTemplate;
import com.gitee.aurora.test.entity.Usss;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuroraService {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;

    public Usss test() {
        String sql = "select * from uss where id = ?";
        int id = 1;
        int[] types = new int[]{java.sql.Types.INTEGER};
        Usss Uss = auroraJdbcTemplate.queryForObject(sql, new Object[]{id}, types, new BeanPropertyRowMapper<>(Usss.class));
        System.out.println(Uss);
        return Uss;
    }

    public List<Usss> testList() throws Exception {
        String sql = "select * from uss";
        List<Usss> uss = auroraJdbcTemplate.queryForMapToList(sql, Usss.class);
        System.out.println(uss);
        return uss;
    }

    @DataSourceRouteKey(type="defaultMysqlDB")
    public List<Usss> testList1() throws Exception {
        String sql = "select * from uss where id > ? and id < ?";
        int[] types = new int[]{java.sql.Types.INTEGER, java.sql.Types.INTEGER};
        List<Usss> usss = auroraJdbcTemplate.queryForMapToList(sql, new Object[]{1, 3}, types, Usss.class);
        System.out.println(usss);
        return usss;
    }

    @DataSourceRouteKey(type="secondMysqlDB")
    public List<Usss> testList2() throws Exception {
        String sql = "select * from uss where id > ? and id < ?";
        int[] types = new int[]{java.sql.Types.INTEGER, java.sql.Types.INTEGER};
        List<Usss> usss = auroraJdbcTemplate.queryForMapToList(sql, new Object[]{1, 3}, types, Usss.class);
        System.out.println(usss);
        return usss;
    }
}
